package org.tpc.yn.service.impl;

import com.alibaba.fastjson2.JSONObject;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.tpc.yn.service.CallService;
import org.tpc.yn.service.SimpleProcedureService;
import org.tpc.yn.utils.JDBCUtil;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;

@Slf4j
@Service
public class CallServiceImpl implements CallService
{

    @Autowired
    private DataSource dataSource;

    @Autowired
    private SimpleProcedureService simpleProcedureService;

    @Override
    public JSONObject call(JSONObject jsonObject) {
        boolean isOracle;
        Connection conn = null;
        try {
            conn = dataSource.getConnection();
            isOracle = JDBCUtil.isOracle(conn);
        }catch (SQLException e){
            throw new RuntimeException(e);
        }finally {
            try {
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        String pProcName = null;
        if(isOracle){
            pProcName = jsonObject.getString("P_PROC_NAME");
        }else{
            pProcName = jsonObject.getString("@PROC_NAME");
        }
        if(pProcName==null){
            throw new RuntimeException("参数错误，未找到P_PROC_NAME参数");
        }
        Map<String, Object> stringObjectMap = simpleProcedureService.callProcedure(pProcName, jsonObject,true);
        return JSONObject.parseObject(JSONObject.toJSONString(stringObjectMap));
    }

}
